package logic
{
	import assets.Globals;
	
	import flash.geom.Point;

	public class FieldCalc
	{
		public static function calcTile(x:int,y:int):Point
		{
			var pom:Point = new Point();
			var pomx:int = x;
			var pomy:int = y;
			
			if((x>Globals.FIELD_OFFSET_X) && (x<(Globals.SCREEN_WIDTH - Globals.FIELD_OFFSET_X)))
			{
				pomx = pomx - Globals.FIELD_OFFSET_X;
			
				pomx = over(pomx , Globals.TILE_WIDTH) * Globals.TILE_WIDTH;
			
				pomx = pomx + Globals.FIELD_OFFSET_X;
			
			}
			else if(x<=Globals.FIELD_OFFSET_X)
			{
				pomx = Globals.FIELD_OFFSET_X;
			}
			else
			{
				pomx = Globals.SCREEN_WIDTH - Globals.FIELD_OFFSET_X - Globals.TILE_WIDTH;
				
			}
			
			if((y>Globals.FIELD_OFFSET_Y) && (y<458))
			{
				pomy = pomy - Globals.FIELD_OFFSET_Y;
				
				pomy = over(pomy , Globals.TILE_HEIGHT) * Globals.TILE_HEIGHT;
				
				pomy = pomy + Globals.FIELD_OFFSET_Y;
				
			}
			else if(y<=Globals.FIELD_OFFSET_Y)
			{
				pomy = Globals.FIELD_OFFSET_Y;
			}
			else
			{
				pomy = 390;
				
			}
			
			pom.x = pomx;
			pom.y = pomy;
			return pom;
		}
		
		public static function calcIndex(x:int,y:int):Point
		{
			var pom:Point = new Point();
			var pomx:int = x;
			var pomy:int = y;
			
			if((x>Globals.FIELD_OFFSET_X) && (x<(Globals.SCREEN_WIDTH - Globals.FIELD_OFFSET_X)))
			{
				pomx = pomx - Globals.FIELD_OFFSET_X;
				
				pomx = over(pomx , Globals.TILE_WIDTH);
			}
			else if(x<Globals.FIELD_OFFSET_X)
			{
				pomx = 0;
			}
			else
			{
				pomx = 9;
				
			}
			
			if((y>Globals.FIELD_OFFSET_Y) && (y<458))
			{
				pomy = pomy - Globals.FIELD_OFFSET_Y;
				
				pomy = over(pomy , Globals.TILE_HEIGHT);
				
				
			}
			else if(y<Globals.FIELD_OFFSET_Y)
			{
				pomy = 0;
			}
			else
			{
				pomy = 5;
				
			}
			
			pom.x = pomx;
			pom.y = pomy;
			return pom;
		}
		
		public static function over(a:int,b:int):int
		{
			var pom:int = 0;
			while(a>b)
			{
				a-=b;
				pom++;
			}
			return pom;
		}
	}
}